<?php

namespace backend\models;

use app\models\ZtContent;
use frontend\models\ZtBlog;
use Yii;
/**
 * This is the model class for table "zt_blog".
 *
 * @property integer $tid
 * @property string $title
 * @property integer $type_id
 * @property string $author
 * @property string $label
 * @property string $create_time
 * @property string $alter_time
 * @property integer $blog_up
 * @property integer $blog_support
 * @property string $content
 * */
class ztArticle extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'zt_blog';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'blog_up', 'blog_support'], 'integer'],
            [['create_time', 'alter_time'], 'safe'],
            [['title', 'author', 'label', 'content'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'tid' => 'Tid',
            'title' => 'Title',
            'type_id' => 'Type ID',
            'author' => 'Author',
            'label' => 'Label',
            'create_time' => 'Create Time',
            'alter_time' => 'Alter Time',
            'blog_up' => 'Blog Up',
            'blog_support' => 'Blog Support',
            'content' => 'Content',
        ];
    }
    public function insertMessage($params,$content){
        $ztblog = new ZtBlog();
        $ztContent = new ZtContent();
        $result = $this->checkName($params['title']);
        if($result){
             return 2;
        }else{
            $params['create_time'] =date('Y-m-d H:i:s');
            $params['alter_time'] =date('Y-m-d H:i:s');
            $ztblog->title  = $params['title'];
            $ztblog->type_id = $params['type_id'];
            $ztblog->author = $params['author'];
            $ztblog->label = $params['label'];
            $ztblog->create_time = $params['create_time'];
            $ztblog->alter_time = $params['alter_time'];
            $ztblog->content = $content;
            return $ztblog->insert();
        }
    }
    private function checkName($title){
        $sql = "select `tid`,`title` FROM `zt_blog` WHERE title = '{$title}'";
        $result  = Yii::$app->db->createCommand($sql)->queryAll();
        if(count($result)-1<=0){
            return false;
        }else {
            return true;
        }
    }
    public function getAll(){
        $sql =  'SELECT
                 a.tid,a.title,a.author,a.label,a.create_time,a.alter_time,b.type,a.content
                 FROM zt_blog AS a 
                 INNER JOIN zt_blog_type AS b on a.type_id=b.id';
        $result  = Yii::$app->db->createCommand($sql)->queryAll();
        return $result;
    }
   public function deleteIndex($index){
        $sql = "delete A from `zt_blog` AS A 
                WHERE A.tid = '{$index}'";
       $result  = Yii::$app->db->createCommand($sql)->execute();
   }
   public function getAllTotal(){
       $ztBlog = new ZtBlog();
      $result  = $ztBlog::find()
           ->count();
      return $result;
   }
}
